<template>
  <div>
    <!-- Breadcrumb 面包屑 导航区-->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item><a href="/">商品管理</a></el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- Card 卡片 卡片视图区-->
    <el-card class="box-card">
      <!-- 添加角色按钮区-->
      <el-row>
        <el-col>
          <el-button type="primary" @click="addDialogVisible = true"
            >添加</el-button
          >
        </el-col>
      </el-row>
      <!-- 表格区-->
      <tree-table
        :data="cateList"
        :selection-type="false"
        :expand-type="false"
        :show-index="true"
        index-text="#"
        border
        :columns="columns"
      >
        <!-- 是否有效模板-->
        <template slot="isok" slot-scope="scope">
          <i
            class="el-icon-success"
            v-if="!scope.row.cat_deleted"
            style="color:green;"
          ></i>
          <i
            class="el-icon-error"
            v-if="scope.row.cat_deleted"
            style="color:red;"
          ></i>
        </template>
        <!-- 排序模板-->
        <template slot="order" slot-scope="scope">
          <el-tag v-if="scope.row.cat_level == 0">一级</el-tag>
          <el-tag type="success" v-if="scope.row.cat_level == 1">二级</el-tag>
          <el-tag type="warning" v-if="scope.row.cat_level == 2">三级</el-tag>
        </template>
        <!-- 操作模板-->
        <template slot="manage" slot-scope="scope">
          <el-button
            type="primary"
            icon="el-icon-edit"
            size="mini"
            @click="showEditDialog(scope.row.id)"
            >编辑</el-button
          >
          <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
            @click="deleteRole(scope.row.id)"
            >删除</el-button
          >
        </template>
      </tree-table>
      <!-- 分页区域-->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data () {
    return {
      // 商品分类的数据列表 数组
      cateList: [],
      total: 0, // 总数据条数
      // 查询条件
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      columns: [
        // 为treetable指定列的定义
        {
          label: '分类名称',
          prop: 'cat_name'
        },
        {
          label: '是否有效',
          prop: 'cat_deleted',
          type: 'template', // 当前列为模板列
          template: 'isok' // 当前列的模板名称
        },
        {
          label: '排序',
          prop: 'cat_level',
          type: 'template', // 当前列为模板列
          template: 'order' // 当前列的模板名称
        },
        {
          label: '操作',
          type: 'template', // 当前列为模板列
          template: 'manage' // 当前列的模板名称
        }
      ]
    }
  },

  methods: {
    // 获取商品分类列表
    async getCateList () {
      const { data: res } = await this.$http.get('/categories/', {
        params: this.queryInfo
      })
      console.log(res)
      if (res.meta.status !== 200) return this.$message.error('获取数据失败')
      this.cateList = res.data.result // 把数据列表赋值给catelist
      this.total = res.data.total
    },
    // 监听分页数量设置
    handleSizeChange (newsize) {
      console.log(newsize)
      console.log(this.queryInfo.pagesize)
      this.queryInfo.pagesize = newsize
      this.getCateList()
    },
    // 监听页码值改变
    handleCurrentChange (newpage) {
      console.log(newpage)
      this.queryInfo.pagenum = newpage
      this.getCateList()
    }
  },

  created () {
    this.getCateList()
  }
}
</script>

<style lang="less" scoped>
.zk-table {
  margin-top: 15px;
}
</style>
